<script setup lang="ts">
const { currentIsDirForm, getDirTree } = defineProps<{
  currentIsDirForm: (type: string) => boolean
  getDirTree: () => BaseModelTreeType
}>()

const model = inject<Ref<BaseModelType>>('model', {} as any)

const parentOptions = computed(() => {
  let options: BaseModelTreeType | null = null
  if (!currentIsDirForm(model.value?.type)) options = getDirTree()
  // 如果当前表单为目录类型表单，则当前表单不能选择自己及其子级为父级
  else
    options = ArrUtil.copyDeep(getDirTree(), {
      propsMap: {
        value: ['value', 'disabled']
      },
      targetPropValPreHandler: {
        // 如果当前表单为目录类型表单，则对其进行禁用
        disabled: (item) => item.value === model.value?._id,
        // 如果当前表单为目录类型表单，则将其子级设置为空
        children: (item, children) => (item.value === model.value?._id ? [] : children)
      }
    })
  // 过滤掉默认目录
  return options.filter((item) => item.value !== DirIdConst.Default)
})

const getParentPropValue = () => {
  const parentPropVal = model.value?.parent
  if (ArrUtil.isArr(parentPropVal)) {
    return ArrUtil.getLast(parentPropVal)
  }
  return parentPropVal
}
defineExpose({
  getParentPropValue
})
</script>

<template>
  <form-item
    label="父级目录"
    prop="parent"
    ipt-type="ElCascader"
    :ipt-props="{
      options: parentOptions,
      props: { checkStrictly: true },
      class: 'w-full',
      placeholder: '请选择父级目录',
      clearable: true
    }"
  />
</template>

<style scoped lang="scss"></style>
